package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author: liangWeiQUQ
 * @Description: 动态菜单权限功能实现
 * @DateTime: 2021/3/11 9:15
 **/
@RestController
@RequestMapping("menu")
@Slf4j
public class MenuController {

    @DubboReference
    private MenuService menuService;


    @GetMapping("findPage")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result findPgae(QueryPageBean queryPageBean) {
        log.info("[菜单---分页查询]data: {}", queryPageBean);
        PageResult pageResult = menuService.findByPage(queryPageBean);

        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, pageResult);
    }


    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu) {
        log.info("[菜单---添加]data: {}", menu);
        menuService.add(menu);
        return new Result(true, MessageConst.ADD_MENU_SUCCESS);
    }

    @PostMapping("delete")
    @PreAuthorize("hasAnyAuthority('MENU_DELETE')")
    public Result deleteById(Integer id) {
        log.info("[菜单---删除]data: {}", id);
        menuService.deleteById(id);
        return new Result(true, MessageConst.DELETE_MENU_SUCCESS);

    }

    @GetMapping("findById")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result findById(Integer id) {
        log.info("[菜单---根据id查询]data: {}", id);
        Menu menu = menuService.findById(id);
        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, menu);
    }

    @PostMapping("update")
    @PreAuthorize("hasAnyAuthority('MENU_UPDATE')")
    public Result edit(@RequestBody Menu menu) {
        log.info("[菜单---修改]data: {}", menu);
        menuService.edit(menu);
        return new Result(true, MessageConst.EDIT_MENU_SUCCESS);
    }

    @GetMapping("findAll")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result findAll() {
        log.info("[菜单---查询所有数据]data: {}");
        List<Menu> all = menuService.findAll();
        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, all);
    }

    @GetMapping("findcheckitemIdsById")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result findcheckitemIdsById(Integer id) {
        CheckItem checkItem = new CheckItem();
        Integer checkItemId = checkItem.getId();
        log.info("[菜单---根据检查项id查询菜单项]data: {}", checkItemId);
        Menu menu = menuService.findcheckitemIdsById(checkItemId);
        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, menu);
    }

    /**
     * @Author: 传闻中的梁大侠
     * @Date: 9:19 2021/3/11
     * @Parms [username]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 动态菜单-->根据用户名获取菜单权限集合
     */
    @GetMapping("getMenuList")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result getMenuList(String username) {
        log.info("[username]:{}", username);
        List<Menu> menuList = menuService.getMenuList(username);
        log.info("[menuList]:{}", menuList);
        return new Result(true, MessageConst.ACTION_SUCCESS, menuList);
    }

    /***
     * @Author: wty
     * @Date: 15:43 2021/3/12
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 查询所有的菜单
     */
    @GetMapping("findAllMenu")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result findAllMenu() {
        List<Menu> menuList = menuService.findAllMenu();
        return new Result(true, "", menuList);
    }
}
